package com.zjlanyun.erp.utils;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v4.app.ActivityCompat;

/**
 * @author: shun
 * @date: 2016-09-27 15:41
 * @Desc: Android6.0检查权限
 */
public class ArdPermission {

    private static ArdPermission ardPermission;
    private PermissionListener permissionListener;

    public static ArdPermission getInstance() {
        if (ardPermission == null) {
            ardPermission = new ArdPermission();
        }
        return ardPermission;
    }

    public static void checkPermission(Context context, int requestCode, String permissions, PermissionListener listener) {
        Activity activity = (Activity) context;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (ActivityCompat.checkSelfPermission(context, permissions) == PackageManager.PERMISSION_GRANTED) {
                if (listener != null)
                    listener.onSuccess(requestCode);

            } else {
                if (ActivityCompat.shouldShowRequestPermissionRationale(activity, permissions)) {
                }
                //申请权限。
                ActivityCompat.requestPermissions(activity, new String[]{permissions}, requestCode);
            }
        } else {
            if (listener != null)
                listener.onSuccess(requestCode);
        }
    }

    public interface PermissionListener {
        void onSuccess(int requestCode);
    }
}
